﻿using System;
using System.Collections;
using System.Configuration;
using System.Globalization;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.Text;
using SQLExecute;
using System.IO;
//using System.Net.Mail;
//using System.Net.Mime;
using System.Text.RegularExpressions;
using DevExpress.Web.ASPxGridView;

public partial class users_list_users : System.Web.UI.Page
{
    User u = new User();
    static int id;
    public string strMessage;
    protected void Page_Init(object sender, EventArgs e)
    {
       BindGrid();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] != null && Session["fullname"] != null)
           {

                Init_Combo();
                BindGrid();
                lUsername.Text = Session["username"].ToString();
            }
            else Response.Redirect("../login.aspx");
        }
    }
    protected void Init_Combo()
    {
        string sql = "select * from tbl_groupuser where id=2 or id=1";
        DataSet ds = SqlHelper.ExecuteDataset(Common.strConnect, CommandType.Text, sql);
        cbGroupUser.DataSource = ds.Tables[0];
        cbGroupUser.DataTextField = "groupname";
        cbGroupUser.DataValueField = "id";
        cbGroupUser.DataBind();
        cbGroupUser.SelectedIndex = 0;
      
        sql = "select * from tbl_phongban";
        ds = SqlHelper.ExecuteDataset(Common.strConnect, CommandType.Text, sql);
        cbPhongBan.DataSource = ds.Tables[0];
        cbPhongBan.DataTextField = "phongban";
        cbPhongBan.DataValueField = "id";
        cbPhongBan.DataBind();
        cbPhongBan.Items.Insert(0, new ListItem("Choose one", "Choose one"));
        cbPhongBan.SelectedIndex = 0;

    }
    protected void BindGrid()
    {

        u.Get_List_Users(gridViewUsers);
    
    }
    protected void ClearTextControls()
    {
        txtFullname.Text = "";
        txtUserName.Text = "";
        txtEmail.Text = "";
        txtNgayNhanViec.Text = ""; txtPassword.Text = "";
        txtRetypePassword.Text = "";

    }
    protected bool CheckValid()
    {
        if (txtFullname.Text == "")
        {
            lMessage.Text = "Vui lòng nhập họ tên!.";

            return false;
        }

        if (!u.ValidMail(txtEmail.Text))
        {

            lMessage.Text = "Địa chỉ email không hợp lệ!.";
            return false;
        }

        if (txtPassword.Text != txtRetypePassword.Text)
        {
            lMessage.Text = "Password & Retype password chưa chính xác.";
            return false;
        }
        return true;
    
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string s = string.Empty;
        string strPassword=string.Empty;

        if (CheckValid())
        {

            Insert_New_User();            
            ClearTextControls();
            BindGrid();
            //u.SendMail("quangtan@vus-etsc.edu.vn", txtEmail.Text, "Welcome to " + txtFullname.Text, 1);

        }
        else

            msgBox.ShowOnPageLoad = true;
        


       
    }

    private void Insert_New_User()
    {
        string s;
        s = u.Encrypt_Pass(txtPassword.Text.Trim());
        u.Password = s;
        u.FullName = txtFullname.Text;
        u.UserName = txtUserName.Text;
        if (cbPhongBan.SelectedIndex > 0)
            u.PhongBanID = Convert.ToInt16(cbPhongBan.SelectedValue);

        u.GroupID = Convert.ToInt16(cbGroupUser.SelectedValue);
        u.Email = txtEmail.Text;

        CultureInfo culture;
        DateTime dt;
        DateTimeStyles styles = DateTimeStyles.None;
        culture = CultureInfo.CreateSpecificCulture("fr-FR");
        DateTime.TryParse(txtNgayNhanViec.Text, culture, styles, out dt);
        u.NgayNhanViec = dt;

        if (chkActive.Checked)
            u.Active = 1;
        else
            u.Active = 0;
        
        u.Insert_New_User(u);
                
    }
    private void Del_User(int nID)
    {
        string sql = "delete tbl_users where id='" + nID + "'";

        SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);



    }
    protected void gdUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Del_User(id);
    }
    protected void gridViewUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
          // id =Convert.ToInt16(e.CommandArgument.ToString());
       

    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
       
        Session.Clear();
        Response.Redirect("../login.aspx");
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        msgBox.ShowOnPageLoad = false; 
    }




    protected void gridViewUsers_RowCommand1(object sender, ASPxGridViewRowCommandEventArgs e)
    {
        id = (int)gridViewUsers.GetRowValues(e.VisibleIndex, "userid");
        string email = (string)gridViewUsers.GetRowValues(e.VisibleIndex, "email");
        if (e.CommandArgs.CommandName== "Xoa")
        {

            Del_User(id);
            BindGrid();
        }
        if (e.CommandArgs.CommandName == "Sua")
        {          
            Response.Redirect("edit_user.aspx?id=" + id);
        }

    }
}
